/**
 * 管理营销活动
 * @type 
 */


/**
 * 定义命名空间，少打些字
 * @type 
 */
var Event = YAHOO.util.Event, 
	Dom = YAHOO.util.Dom, 
	JSON = YAHOO.lang.JSON, 
	Connect = YAHOO.util.Connect,
	DataSource = YAHOO.util.DataSource


/**
 * 简化document.getElementById的写法
 * @param {} tagId
 * @return {}
 */
var element = function(tagId) {
	return document.getElementById(tagId);
}

/**
 * 全局变量
 * @type Number
 */
var promotionId=1;			//活动编号
var baseData=null;			//基本内容
var attachmentList=[];		//附件
var gift=null;				//礼品————暂不处理
var questionList=[];		//调查问题的清单
var custGroupList=[];		//目标用户群清单
var replyList=[];			//回复清单
var resultList=[];			//执行结果清单

/**
 * 刷新基本内容
 */
var refreshBase = function() {
	var callback = {
		cache : false,
		success : function(o) {
			var promotion = JSON.parse(o.responseText);
			element('id').innerText = promotion.id;
			element('name').innerText = promotion.name;
		}
	}
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/base', callback);
}

refreshBase();		//页面装载时获取基本内容


var refreshAttachment = function() {	
	var callback = {
		cache : false,
		success : function(o) {
			var list=[];
			list = JSON.parse(o.responseText);
			var html='';
			for (var i = 0, len = list.length; i < len; ++i) {
				html=html+'<a href="/store/upload/'+list[i].fileName+'">'+list[i].fileName+'</a>&nbsp;&nbsp;&nbsp;';
			}
			element('attachment').innerHTML=html;
		}
	}
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/attachmentList', callback);	
}

refreshAttachment();


YAHOO.widget.Chart.SWFURL = "/yui/charts/assets/charts.swf";

var jsonData = new YAHOO.util.DataSource("/promotion/1/resultChart/1");
jsonData.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
jsonData.responseSchema = {
	fields : ["q", "r"]
};

var mychart = new YAHOO.widget.PieChart(
	"chart", 
	jsonData, 
	{
		dataField : "r",
		categoryField : "q",
		style : {
			padding : 20,
			legend : {
				display : "right",
				padding : 10,
				spacing : 5,
				font : {
					family : "Arial",
					size : 13
				}
			}
		},
		//only needed for flash player express install
		expressInstall : "/yui/assets/expressinstall.swf"
	}
);


//----------------------展示调查问卷--------------------------------
var refreshQuestionnaire=function(){
	var callback = {
		cache : false,
		success : function(o) {
			//解析数据
			questionList = JSON.parse(o.responseText);
			//展示问卷
			var div=element('questionnaire');
			div.innerHTML='';
			div.style.display='none';
			for (var i = 0, len = questionList.length; i < len; ++i) {
				var q=[];
				q=questionList[i];
				//每个问题一个fieldset
				var fieldSet = document.createElement('fieldset');
				div.appendChild(fieldSet);
				var legend=document.createElement('legend');
				//问题的第一行是提问，后面是选择答案
				legend.innerText=q[0];
				fieldSet.appendChild(legend);
				var ul=document.createElement('ul');
				fieldSet.appendChild(ul);
				for (var j = 1, lenq = q.length; j < lenq; ++j) {
					var li=document.createElement('li');
					ul.appendChild(li);
					li.innerText=q[j];
				}
			}
			div.style.display='';

			//填充问卷表单，每个问题用空行隔开，问题的第一行是提问
			var textarea=element('qList');
			var lines='';
			for (var i = 0, len = questionList.length; i < len; ++i) {
				var q=[];
				q=questionList[i];
				for (var j = 0, lenq = q.length; j < lenq; ++j) {
					lines=lines+q[j]+'\r\n';
				}
				lines=lines+'\r\n';
			}
			textarea.innerText=lines;
		}
	}
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/questionList', callback);	
};


refreshQuestionnaire();


//目标客户
//包含一个清单和一个隐藏界面(YUI panel)，当用户点击清单中的条目时，工具条展现出来，在其上给出编辑、删除等功能


var showCustCommandPanel= function(custId) {
	var panel=new YAHOO.widget.Panel(
		'custCommandPanel',
		{	width:'320px',
			draggable:false,
			close:true
		}
		);
	panel.setHeader('查看客户');
	var saveButton='<button onclick="saveCust('+custId+')">保存</button>';
	var deleteButton='<button onclick="deleteCust('+custId+')">删除</button>';
	//TODO 分区域级别、文件清单和单个客户填充表单
	var html='<form id="custForm">';
	html=html+'<label for="desc">描述</label><input name="desc" value="测试客户组描述"/>'
	html=html+'</form>';
	panel.setBody(html);
	panel.setFooter(saveButton+deleteButton);
	panel.render('custList');
	panel.show();
};

var deleteCust=function(custId){
	alert('去除'+custId+'吗');
}

var refreshCust=function(o) {
	var callback = {
		cache : false,
		success : function(o) {
			var div=element('custList');
			//解析数据
			var custList=[];
			custList = JSON.parse(o.responseText);
			//填充显示区	
			var html='<ul>';
			for (var i = 0, len = custList.length; i < len; ++i) {
				var cust=custList[i];
				//var editButton='<button onclick="editCust('+cust.id+')">编辑</button>';
				//var deleteButton='<button onclick="deleteCust('+cust.id+')">删除</button>';
				//var command='<span id="custCommand'+cust.id+'">'+editButton+deleteButton+'</span>';
				html=html+'<li id="cust'+cust.id+'" onclick="javascript:showCustCommandPanel('+cust.id+')">'+cust.desc+'</li>';
				//html=html+'<li id="cust'+cust.id+'" onmouseover="javascript:element(\'custCommand'+cust.id+'\').style.display=\'\'">'+cust.desc+'&nbsp;&nbsp;&nbsp;'+command+'</li>';
			}
			html=html+'</ul>';
			div.innerHTML=html;
		}
	};
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/custList', callback);
};

refreshCust();


var refreshReply=function() {
	var callback = {
		cache : false,
		success : function(o) {
			//var div=element('replyList');
			var replyList=[];
			replyList=JSON.parse(o.responseText);
			for (var i = 0, len = replyList.length; i < len; ++i) {
				reply=replyList[i];
				var module=new YAHOO.widget.Module(
					'reply'+reply.id,
					{visible:true}
				);
				module.setHeader(i);
				module.setBody('回复'+i);
				module.render('replyList');
			}
		}
	};
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/replyList', callback);
}
refreshReply();